//背景移动
let setBg;
function bgroundMove(){
	let gameW = $(".gameWrap");
	let bgImg = $(".gamebg");
	let _li = $(".gamebg li");
	let leftM = 0; 
	let imgw = _li.width();
	console.log(imgw);
	let cloneImg = _li.clone();
	console.log(cloneImg);
	bgImg.append(cloneImg);
	setBg = setInterval(function(){
		leftM--;
			bgImg.css("transform","translateX("+leftM+"px"+")");
		if(leftM<-imgw){
			leftM = 0;
		}
	},20)
}
	bgroundMove();
//云移动
let setCloud1;
function cloudMove(){
	let gameW = $(".gameWrap");
	let cloud = $(".cloud");
	let _li = $(".cloud li");
	let leftM = 300; 
	let imgw = _li.width();
	console.log(imgw);
	let cloneImg = _li.clone();
	console.log(cloneImg);
	cloud.append(cloneImg);
	setCloud1 = setInterval(function(){
		leftM--;
			cloud.css("transform","translateX("+leftM+"px"+")");
		if(leftM<-imgw){
//			clearInterval(setCloud);
			leftM = 0;
		}
	},20)
}
cloudMove();
let setCloud2;
function cloudMove2(){
	let gameW = $(".gameWrap");
	let cloud = $(".cloud2");
	let _li = $(".cloud2 li");
	let leftM = 1000; 
	let imgw = _li.width();
	console.log(imgw);
	let cloneImg = _li.clone();
	console.log(cloneImg);
	cloud.append(cloneImg);
	setCloud2 = setInterval(function(){
		leftM--;
			cloud.css("transform","translateX("+leftM+"px"+")");
		if(leftM<-imgw){
//			clearInterval(setCloud2);
			leftM = 0;
		}
	},15)
}
cloudMove2();


let gameH = $(".gameWrap").height(),//获取屏幕高
		per = $(".person"),//人物盒子
		perImg = $(".person img"),//图片
		leftM = 0,//定义图片变换移动的值
		r = 2.5,//rem转换值
		imgW  = perImg.width(),//获取图片的宽
		setR;
	//人物跑动动画
	function running(){
		setR = setInterval(function(){
			leftM -= r;
			perImg.css("left",leftM+"rem");
			if(leftM<= -r*5){
				leftM =0;
			}
		},80)
	}
	running();
	//跑动动画清除
	function clearRun(){
		clearInterval(setR);
	}
	
	
	//点击人物跳动
	function perjump(){
		let clickNum =0;
		let isJump = true;
		$(document).on("touchstart",function(e){
			num = per.offset().top-(per.height()*1.2);
			clearRun();
			clickNum++;
			console.log(clickNum);
			if(clickNum>2){
				isJump = false;
				$("#jump")[0].pause();
			}
			if(isJump){
				//人物跳动
				$("#jump")[0].currentTime=0;//音频进度条回到0
				$("#jump")[0].play();//音频播放
				if(per.is(":animated")){
					per.stop(true,false);
				}
				perImg.css("left",-r*6+"rem");
				per.animate({
					"top":num+"px"
				},500)
				fall(1500);
			}
		})
	}
	
	
	//人物自动落下
	let setD;
	let lifeNum = 0;
	function fall(time){
		clearRun();
		perImg.css("left",-r*6+"rem");
		per.animate({
			"top":gameH+"px"
		},{
			easimg:"easeInQuad",
			duration:time,
			step:function(now,fx){
				let 
					road1 = $(".Road1"),
					road2 = $(".Road2"),
					roadH1 = parseInt(road1.offset().top),
					roadH2 = parseInt(road2.offset().top),
					per = $(".person"),
					perH = parseInt(per.offset().top+per.height()),
					isRun1 = bump(per,road1),
					isRun2 = bump(per,road2);
				if(isRun1){
					per.stop();
					running();
					perjump();
					per.css("top",roadH1+"px");
					let runDown = setInterval(function(){
						if(road1.offset().left+road1.width()<per.offset().left+per.width()/2){
							clearRun();
							perImg.css("left",-r*6+"rem");
							per.animate({
								"top":gameH+"px"
							},{
								easing: 'easeInQuad',
			    				duration: 800,
							});
							clearInterval(runDown);
						}	
					})
				}
				if(isRun2){
					per.stop();
					running();
					perjump();
					per.css("top",roadH2+"px");
					let runDown = setInterval(function(){
						if(road2.offset().left+road2.width()<per.offset().left+per.width()/2){
							clearRun();
							perImg.css("left",-r*6+"rem");
							per.animate({
								"top":gameH+"px"
							},{
								easing: 'easeInQuad',
			    				duration: 800,
							});
							clearInterval(runDown);
						}	
					})
				}
			}
		})
		
		
		
	}
	

	
//	perjump()
	//行道
	function roadFun(){
		let road = $(".road"),
			per = $(".person"),
			roadH = ((road.offset().top-per.height())/gameH*100),//行道上距离屏幕顶部的距离
			roadMove = 0,
			roadW = road.width(),
			roadL = 0,//行道递减初始值
			perL = parseInt(per.offset().left+per.width()/2);//人物到屏幕的左边距离
			per.css("top",roadH+"%");//人物的位置
		//移动行道
		road.animate({
			"left":-roadW+"px"
		},6000,"linear")
		let setT = setTimeout(function(){
			fall(1500);
			perjump(0);
			clearTimeout(setT);
		},5100)
}
		
		//设置行道高
		let setroad1;
		function roadLoop1(){
//			console.log(res);
			Road1();//调用随机生成礼物
			let road = $(".Road1");
			let roadW = road.width();
			let roadH = parseInt(Math.random()*6+7);
			let Goods = $(".Road1 .Goods");
			let roadT = road.offset().top;
			road.css("top",roadH+"rem");
			road.animate({
				"left":-roadW+"px"
			},4000,"linear",function(){
				road.css("top",roadH+"rem"),
				road.css("left",100+"%"),
				Goods.children().remove()
			})
			setroad1 = setTimeout(function(){
				roadLoop2();
				clearTimeout(setR)
			},3200)
//			let res;
		}
		let setroad2;
		function roadLoop2(){
			Road2();
			let road = $(".Road2");
			let roadW = road.width();
			let roadH = parseInt(Math.random()*5+13);
			let Goods = $(".Road2 .Goods");
			road.css("top",roadH+"rem");
			road.animate({
				"left":-roadW+"px"
			},4000,"linear",function(){
				road.css("top",roadH+"rem"),
				road.css("left",100+"%"),
				Goods.children().remove()
			})
			setroad2 = setTimeout(function(){
				roadLoop1();
				clearTimeout(setR)
			},3200)
		}
		let serT = setTimeout(function(){
			roadLoop1();
			clearTimeout(serT);
		},4200)
		
		
	//随机生成礼物1
	function Road1(){
		let Goods = $(".Road1 .Goods");
		let roadImg = $(".roadImg");
		for(var i=1;i<4;i++){
			let imgM = parseInt(Math.random()*7+1);//随机奖品
			if(imgM==7){
				let goods =`<div class="goodstone gift" data-img="1" style="top:-1.1rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
				Goods.append(goods);
			}else{
				var imgHeight = -(parseInt(Math.random()*4+6));//随机高度
//				console.log(imgHeight);
				if(imgM==5){
					let goods =`<div class="goods gift" data-img="3" style="top:${imgHeight}rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
					Goods.append(goods);
				}else{
					let goods =`<div class="goods gift" data-img="2" style="top:${imgHeight}rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
					Goods.append(goods);
				}
			}
		}
	}
	//随机生成礼物2
	function Road2(){
		let Goods = $(".Road2 .Goods");
		let roadImg = $(".roadImg");
		for(var i=1;i<4;i++){
			let imgM = parseInt(Math.random()*7+1);//随机奖品
			if(imgM==7){
				let goods =`<div class="goodstone gift" data-img="1" style="top:-1.1rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
				Goods.append(goods);
			}else{
				var imgHeight = -(parseInt(Math.random()*4+6));//随机高度
//				console.log(imgHeight);
				if(imgM==5){
					let goods =`<div class="goods gift" data-img="3" style="top:${imgHeight}rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
					Goods.append(goods);
				}else{
					let goods =`<div class="goods gift" data-img="2" style="top:${imgHeight}rem;left:${(i-1)*5}rem"><img src="img/yx/prop${imgM}.png"/></div`
					Goods.append(goods);
				}
			}
		}
	}
		


//碰撞检测
function bump(obj1,obj2){
	let obj1T = obj1.offset().top;
	let obj1L = obj1.offset().left;
	let obj2T = obj2.offset().top;
	let obj2L = obj2.offset().left;
	let road1 = $(".Road1");
	let road2 = $(".Road2");
//	let goods = $(".goods");
//	let goodstone = $(".goodstone");
	let t1 = obj1T
		r1 = obj1L+obj1.width(),
		b1 = obj1T+obj1.height(),
		l1 = obj1L;
	let t2 = obj2T,
		r2 = obj2L+obj2.width(),
		b2 = obj2T+obj2.height(),
		l2 = obj2L;
	if(obj2.hasClass("Road1")||obj2.hasClass("Road2")){
		if(t1+$(".person").height()>b2||b1<t2||r1<l2||l1>r2){
			return false;
		}else{
			return true;
		}
	}else if(obj2.hasClass("gift")){
		if(t1>b2||r1<l2||b1<t2||l1>r2){
			return false;
		}else{
			return true;
		}
	}
}

//碰撞礼物

let setP = setInterval(function(){
	let gift = $(".gift");
	for(var i=0;i<gift.length;i++){
		let ind = gift.eq(i);
		let daImg = ind.data().img;
//		console.log(idx);
		giftDump(bump(per,ind),ind,daImg);
	}
},220)
let giftNum =0;
var totalScore = 0;
let scoreN =0,
    scoreS =0;
function giftDump(res,val,idx){
	if(res){
		giftNum++;
		if(giftNum==1){
//			console.log(idx);
			switch(idx){
				case 1:
					//滚动
					val.animate({
						opacity:0
					},1000)
					clearRun();
					$(document).off("touchstart");
					
					perImg.css("left",-r*7+"rem");
					$(".person").addClass("roll");
					$("#trip")[0].currentTime=0;
					$("#trip")[0].play();
					let setRoll = setTimeout(function(){
						$(".person").removeClass("roll");
						perjump();
					},500)
					break;
					
				case 2:
					scoreN++;
					if(scoreN==1){
						console.log(2);
						totalScore+=1;
						$("#scoreM")[0].currentTime=0;
						$("#scoreM")[0].play();
						val.animate({
							width:0,
							height:0
						},1000);
						scoreN=0;
					}
					$(".score").text(totalScore);
					break;
				case 3:
					scoreS++;
					if(scoreS==1){
						console.log(3);
						totalScore+=5;
						$("#scoreM")[0].currentTime=0;
						$("#scoreM")[0].play();
						val.animate({
							width:0,
							height:0
						},1000)
						scoreS=0;
					}
					$(".score").text(totalScore);
					break;
			}
		}else{
			giftNum=0;
		}
		
		
		}
}




function playRun(){
	$(".person").animate({
		"left":10+"%"
	},1000)
	roadFun()
}
playRun();


let liftSet = setInterval(function(){
			let life =$(".lifeImg li:first");
			if(parseInt(per.offset().top)>=gameH){
				console.log("掉下去了",parseInt(per.offset().top),gameH);
				per.stop(true,false);
				per.css("top",-per.height()+"px");
				console.log(life);
				life.remove();
				lifeNum++;
				console.log(lifeNum)
				fall(3000);
				if(lifeNum>2){
					$("#gameO")[0].currentTime=0;
					$("#gameO")[0].play();
					$(".person").stop();
					clearInterval(setBg);//背景停止
					clearInterval(setCloud1);//云一停止
					clearInterval(setCloud2);//云二停止
					clearRun();//清除人物跑动
					clearTimeout(setroad1);//清除行道
					clearTimeout(setroad2);//清除行道
					$(".Road1").stop();//行道停止移动
					$(".Road2").stop();//行道停止移动
					$(".person").css("top",-per.height()+"px");
					$("#bgmusic")[0].pause();
					clearInterval(liftSet);
					$(".overWrap").css("display","flex");
				}
			}
		},200)


//游戏结束
function endFun(){
	//再玩一次
	$(".againP").on("touchstart",function(){
		let aHref = $(".againP").attr("href");
		window.open(aHref,"_self");
	})
	//查看排名
	$(".look_rank").on("touchstart",function(){
		$(".rank_wrap").css("display","block");
	})
	$(".close").on("touchstart",function(){
		$(".rank_wrap").css("display","none");
	})
	//分享按钮
	$(".shareBtn").on("touchstart",function(){
		$(".share").css("display","block");
	})
	$(".share").on("touchstart",function(){
		$(".share").css("display","none");
	})
	
}
endFun();


//控制背景音量
$(function(){
	$("#bgmusic")[0].play();
	$("#bgmusic")[0].volume=0.1;
})
